'use strict';

const Controller = require('egg').Controller;
const moment = require('moment')

class Apply extends Controller {
  async applyList() {
    const {
      ctx
    } = this;
    const Op = ctx.model.Sequelize.Op
    let page = ctx.request.body.page
    let pageshow = ctx.request.body.pageshow
    let sex = ctx.request.body.sex
    let stime = moment(ctx.request.body.stime).unix()
    let etime = moment(ctx.request.body.etime).unix()
    let condition = {}

    if (!sex == '') {
      Object.assign(condition, {
        sex
      })
    }
    if (stime && etime) {
      Object.assign(condition, {
        created_at: {
          [Op.between]: [stime, etime]
        }
      })
    }
    let user = await ctx.model.Test.findAndCountAll({
      where: condition,
      order: [
        ['created_at', 'DESC'],
      ],
      limit: pageshow,
      offset: (page - 1) * pageshow
    })

    return ctx.body = {
      code: 200,
      data: {
        list: user.rows.map(item => {
          let obj = JSON.parse(JSON.stringify(item))
          return {
            ...obj,
            created_at: moment(obj.created_at * 1000).format('YYYY-MM-DD HH:mm:ss')
          }
        }),
        count: user.count
      },
      msg: '操作成功'
    }
  }
}
module.exports = Apply;